Fix panic when TURN server returns non-UTF-8 bytes in STUN error reason phrase#64
Open
danielbotros wants to merge 3 commits intowebrtc-rs:masterfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi! We recently ran into a panic with the
webrtc-rscrate. I wanted to share some details about it and propose a simple fix. Thanks!Background
STUN error responses carry a human-readable reason phrase (e.g.
"Unauthorized","Stale Nonce"). In thertc-stuncrate,ErrorCodeAttributestores this phrase as raw bytes (Vec<u8>) parsed directly off the wire.We upgraded our CoTURN server to
latest, which has a bug where non UTF-8 reason phrases can end up being sent (wheninclude_reason_stringis false, the STUN message can contain uninitialized stack bytes). This caused our SDKs, which rely onwebrtc-rs, to crash due to a panic in thertc-turncrate.The Bug
The Display implementation for
ErrorCodeAttributeattempts to decode those bytes as UTF-8, and returnsErr(fmt::Error {})if decoding failed.Upstream in the
rtc-turncrate we try toformat!the error returned by theErrorCodeAttributeDisplay impl which causes a panic.get_fromsimply parses the raw STUN message bytes into the struct fields, so does not error on invalid UTF-8 bytes.ex.:
rtc/rtc-turn/src/client/relay.rsProposed Fix
In the Display impl for
ErrorCodeAttribute, we useString::from_utf8_lossyinstead ofString::from_utf8, which replaces the invalid bytes rather than erroring.The reason phrase is purely informational and has no effect on protocol logic, so this seemed like the least invasive fix. We expect the reason phrase to be valid UTF-8, so the loss of invalid bytes in exchange of preventing a panic seems like a good trade off.
We also considered modifying
get_fromto validate the UTF-8 bytes, but this is a much more disruptive change and a malformed reason phrase is not worth mishandling a potentially valid error response over.Using Debug formatting could have also fixed this panic, but it renders reason phrases as raw byte arrays defeating the purpose of human-readable error messages for the common case where bytes are valid UTF-8.
Testing
There was no existing test file. I created one and added two new test cases to catch any regressions for this.
test_display_valid_utf8--> asserts valid UTF-8 reason phrase displays as expectedtest_display_invalid_utf8_does_not_panic--> asserts invalid UTF-8 reason phrase does not panic and invalid bytes are replaced